//
//  HZPeiziCancelOrderVC.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/11/29.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziCancelOrderVC.h"
#import "HZPeiziQueryView.h"
#import "MYHttp.h"

@interface HZPeiziCancelOrderVC ()
@property (nonatomic, strong)HZPeiziQueryView *pQueryView;
@end

@implementation HZPeiziCancelOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"撤单";
    
    //关联逻辑管理对象
    self.logicManager = [HZBaseLogjcManager new];
    self.view.logicManager = self.logicManager;
    
    _pQueryView = [HZPeiziQueryView new];
    _pQueryView.nQueryType = kQuery_cancel_Type;
    [self.view addSubview:_pQueryView];
    [_pQueryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [self p_registerBlock];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self sendRequest];
}

- (void)sendRequest
{
    NSString *date = @"1";
    NSString *status = @"1";
    NSString *total = @"0";
    [self p_sendExcutingRequestWithDate:date sellStatus:status total:total];
}
#pragma mark -
#pragma mark request
- (void)p_sendExcutingRequestWithDate:(NSString *)date
                           sellStatus:(NSString *)sellStatus
                                total:(NSString *)total
{
    [SVProgressHUD show];
    
    NSArray *array = @[@{@"funcid":@"2", @"showtype":@"2",@"session":[HZUserManager manager].peiziAccountSession}];
    [MYHttp post:kAPIPeiziHost params:array success:^(id response) {
        NSMutableArray *tempArray = [NSMutableArray array];
        if([response isKindOfClass:[NSArray class]]){
            for (NSDictionary *dict in response) {
                HZPeiziCancelOrderListModel *model = [HZPeiziCancelOrderListModel mj_objectWithKeyValues:dict];
                [tempArray addObject:model];
            }
        }
        [_pQueryView updateNewData:tempArray];
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain];
    }];
}

#pragma mark -
#pragma mark blockAction
- (void)p_registerBlock
{
    __weak typeof (self)weakSelf = self;
    [self.logicManager mapLogic:kLogicRouterExcute_clickCancel toBlock:^id(NSDictionary *params, id other) {
        [weakSelf clickCancelBtn:other];
        return nil;
    }];
}
- (void)clickCancelBtn:(HZPeiziCancelOrderListModel *)model
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确定要撤销该笔委托吗？" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [SVProgressHUD show];
        NSArray *array = @[@{@"funcid":@"11", @"orderno":model.sno,@"session":[HZUserManager manager].peiziAccountSession}];
        [MYHttp post:kAPIPeiziHost params:array success:^(id response) {
            [SVProgressHUD showSuccessWithStatus:@"撤单委托提交成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self p_sendExcutingRequestWithDate:@"" sellStatus:@"" total:@""];
            });
        } failure:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVC addAction:actionOk];
    [alertVC addAction:actionCancel];
    [self presentViewController:alertVC animated:YES completion:nil];
}
@end
